using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Close.io.API
{
	public class OpportunityQueryBuilder : QueryBuilderBase
	{
		public OpportunityQueryBuilder(string leadId)
			: base(leadId)
		{
		}


		public OpportunityQueryBuilder SetDateCreatedExpression(IDateExpression dateExpression)
		{
			Add(dateExpression.GetKey(), dateExpression.GetValue());
			return this;
		}

		public OpportunityQueryBuilder SetDateUpdatedExpression(IDateExpression dateExpression)
		{
			Add(dateExpression.GetKey(), dateExpression.GetValue());
			return this;
		}

		public OpportunityQueryBuilder SetDateWonExpression(IDateExpression dateExpression)
		{
			Add(dateExpression.GetKey(), dateExpression.GetValue());
			return this;
		}


		public OpportunityQueryBuilder SetQuery(string query)
		{
			Add("_query", query);
			return this;
		}

		public OpportunityQueryBuilder SetStatusExpression(OpportunityStatusExpression statusExpression)
		{
			if (!string.IsNullOrWhiteSpace(statusExpression.Status))
			{
				Remove("status_label__in");


				Add("status_label", statusExpression.Status);
			}

			if (!string.IsNullOrWhiteSpace(statusExpression.StatusId))
			{
				Remove("status_id__in");


				Add("status_id", statusExpression.StatusId);
			}

			if (statusExpression.StatusType != OpportunityStatusType.None)
			{
				Remove("status_type__in");

				Add("status_type", statusExpression.StatusType.GetAttributeOfType<DescriptionAttribute>().Description);
			}
			return this;
		}

		public OpportunityQueryBuilder SetValuePeriod(OpportuntityValuePeriod valuePeriod)
		{
			Remove("value_period__in");

			Add("value_period", valuePeriod.GetAttributeOfType<DescriptionAttribute>().Description);
			return this;
		}


		public OpportunityQueryBuilder SetOrderBy(OpportunitySortField orderBy)
		{
			if (orderBy != OpportunitySortField.None)
				Add("order_by", orderBy.GetAttributeOfType<DescriptionAttribute>().Description);
			return this;
		}

		public OpportunityQueryBuilder SetGroupBy(GroupBy groupBy)
		{
			if (groupBy != GroupBy.None)
				Add("group_by", groupBy.GetAttributeOfType<DescriptionAttribute>().Description);
			return this;
		}

		public OpportunityQueryBuilder SetFields(CloseIoFieldListBuilder<Opportunity> fields)
		{
			if (fields == null) throw new ArgumentNullException("fields");

			Add("_fields", string.Join(",", fields.GetFields()));
			return this;
		}

		public OpportunityQueryBuilder SetUserId(string userId)
		{
			Remove("user_id__in");

			Add("user_id", userId);
			return this;
		}

		public OpportunityQueryBuilder SetUserIdIn(IEnumerable<string> userIds)
		{
			Remove("user_id");

			Add("user_id__in", string.Join(",", userIds));

			return this;
		}

		public OpportunityQueryBuilder SetStatusIdIn(IEnumerable<string> statusIds)
		{
			Remove("status_id");

			Add("status_id__in", string.Join(",", statusIds));

			return this;
		}

		public OpportunityQueryBuilder SetStatusIn(IEnumerable<string> statuses)
		{
			Remove("status_label");

			Add("status_label__in", string.Join(",", statuses));

			return this;
		}

		public OpportunityQueryBuilder SetStatusTypeIn(IEnumerable<string> statusTypes)
		{
			Remove("status_type");

			Add("status_type__in", string.Join(",", statusTypes));

			return this;
		}

		public OpportunityQueryBuilder SetValuePeriodIn(IEnumerable<string> valuePeriods)
		{
			Remove("value_period");

			Add("value_period__in", string.Join(",", valuePeriods));
			return this;
		}
 
	}
}